#!/bin/sh
[ -n "${IPKG_INSTROOT}" ] && exit 0
_cored=/usr/bin/relayway-cored
_backup=/usr/bin/relayway-cored.pre-upgrade
_version=/usr/share/relayway-cored/VERSION
_version_backup=/usr/share/relayway-cored/VERSION.pre-upgrade
_version_missing=/usr/share/relayway-cored/VERSION.pre-upgrade.missing
_www=/usr/share/relayway-cored/www
_www_backup=/usr/share/relayway-cored/www.pre-upgrade
_www_missing=/usr/share/relayway-cored/www.pre-upgrade.missing

# A failed postinst deliberately keeps the binary rollback copy. The first
# rollback consumes the Web backup with mv, so a retry must rebuild that part
# of the snapshot from the restored live generation before unpacking again.
# Never overwrite an existing last-known binary/version snapshot; refuse when
# the remaining artifacts describe different generations.
relayway_rebuild_rollback_web_snapshot() {
    _has_web_backup=0
    _has_web_missing=0
    if [ -e "$_www_backup" ] || [ -L "$_www_backup" ]; then
        [ -d "$_www_backup" ] && [ ! -L "$_www_backup" ] || return 1
        _has_web_backup=1
    fi
    if [ -e "$_www_missing" ] || [ -L "$_www_missing" ]; then
        [ -f "$_www_missing" ] && [ ! -L "$_www_missing" ] || return 1
        _has_web_missing=1
    fi
    [ "$_has_web_backup" = "0" ] || [ "$_has_web_missing" = "0" ] || return 1
    _has_version_backup=0
    _has_version_missing=0
    if [ -e "$_version_backup" ] || [ -L "$_version_backup" ]; then
        [ -f "$_version_backup" ] && [ ! -L "$_version_backup" ] || return 1
        _has_version_backup=1
    fi
    if [ -e "$_version_missing" ] || [ -L "$_version_missing" ]; then
        [ -f "$_version_missing" ] && [ ! -L "$_version_missing" ] || return 1
        _has_version_missing=1
    fi
    [ "$_has_version_backup" = "0" ] || [ "$_has_version_missing" = "0" ] || return 1
    if [ "$_has_web_backup" = "1" ] || [ "$_has_web_missing" = "1" ]; then
        [ "$_has_version_backup" = "1" ] || [ "$_has_version_missing" = "1" ] || return 1
        return 0
    fi

    # If the version snapshot survived the previous rollback, prove that the
    # restored live marker is the same generation before rebuilding Web.
    if [ -f "$_version_backup" ]; then
        if [ -e "$_version" ] || [ -L "$_version" ]; then
            [ -f "$_version" ] && [ ! -L "$_version" ] || return 1
            _live_version="$(tr -d '\r\n' < "$_version")" || return 1
            _saved_version="$(tr -d '\r\n' < "$_version_backup")" || return 1
            [ "$_live_version" = "$_saved_version" ] || return 1
        else
            return 1
        fi
    elif [ -f "$_version_missing" ]; then
        [ -e "$_version" ] || [ -L "$_version" ] && return 1
    else
        # A rollback artifact created by an older maintainer script may have
        # no VERSION snapshot. Capture the restored generation once, without
        # replacing any existing artifact.
        _made_version_snapshot=0
        if [ -e "$_version" ] || [ -L "$_version" ]; then
            [ -f "$_version" ] && [ ! -L "$_version" ] || return 1
            _version_tmp="${_version_backup}.tmp.$$"
            rm -f "$_version_tmp"
            if ! cp -p "$_version" "$_version_tmp" || ! chmod 0644 "$_version_tmp" || ! mv -f "$_version_tmp" "$_version_backup"; then
                rm -f "$_version_tmp"
                return 1
            fi
            _made_version_snapshot=1
        else
            : > "$_version_missing" || return 1
            _made_version_snapshot=2
        fi
    fi

    if [ -e "$_www" ] || [ -L "$_www" ]; then
        [ -d "$_www" ] && [ ! -L "$_www" ] || {
            [ "${_made_version_snapshot:-0}" = "1" ] && rm -f "$_version_backup"
            [ "${_made_version_snapshot:-0}" = "2" ] && rm -f "$_version_missing"
            return 1
        }
        if ! mv "$_www" "$_www_backup" || ! mkdir -m 0755 "$_www"; then
            mv "$_www_backup" "$_www" 2>/dev/null || true
            [ "${_made_version_snapshot:-0}" = "1" ] && rm -f "$_version_backup"
            [ "${_made_version_snapshot:-0}" = "2" ] && rm -f "$_version_missing"
            return 1
        fi
    else
        : > "$_www_missing" || {
            [ "${_made_version_snapshot:-0}" = "1" ] && rm -f "$_version_backup"
            [ "${_made_version_snapshot:-0}" = "2" ] && rm -f "$_version_missing"
            return 1
        }
    fi
    return 0
}

if [ -f "$_backup" ] && [ -x "$_backup" ]; then
    # A prior interrupted/failed upgrade deliberately retained its last known
    # rollback artifact. Rebuild only a consumed Web half of that snapshot;
    # never overwrite the retained binary/version half.
    if ! relayway_rebuild_rollback_web_snapshot; then
        echo "relayway: retained rollback snapshot is incomplete or inconsistent; refusing upgrade" >&2
        exit 1
    fi
    echo "relayway: preserving existing cored rollback binary at $_backup"
    exit 0
fi
if [ -e "$_www_backup" ] || [ -e "$_www_missing" ] ||
   [ -e "$_version_backup" ] || [ -e "$_version_missing" ]; then
    echo "relayway: previous rollback artifacts already exist; refusing upgrade" >&2
    exit 1
fi
if [ -f "$_cored" ] && [ -x "$_cored" ]; then
    _tmp="${_backup}.tmp.$$"
    rm -f "$_tmp"
    if ! cp -p "$_cored" "$_tmp" || ! chmod 0755 "$_tmp" || ! mv -f "$_tmp" "$_backup"; then
        rm -f "$_tmp"
        echo "relayway: could not preserve the current cored binary; refusing upgrade before overwrite" >&2
        exit 1
    fi
    if [ -e "$_version" ] || [ -L "$_version" ]; then
        [ -f "$_version" ] && [ ! -L "$_version" ] || {
            rm -f "$_tmp" "$_backup"
            echo "relayway: existing VERSION marker is not a regular file; refusing upgrade" >&2
            exit 1
        }
        _version_tmp="${_version_backup}.tmp.$$"
        rm -f "$_version_tmp"
        if ! cp -p "$_version" "$_version_tmp" || ! chmod 0644 "$_version_tmp" || ! mv -f "$_version_tmp" "$_version_backup"; then
            rm -f "$_version_tmp" "$_backup"
            echo "relayway: could not preserve the current VERSION marker; refusing upgrade before overwrite" >&2
            exit 1
        fi
    else
        : > "$_version_missing" || {
            rm -f "$_tmp" "$_backup"
            echo "relayway: could not record missing VERSION marker; refusing upgrade" >&2
            exit 1
        }
    fi
elif [ -e "$_backup" ]; then
    echo "relayway: unusable cored rollback artifact exists at $_backup; refusing upgrade" >&2
    exit 1
fi
if [ -e "$_www" ] || [ -L "$_www" ]; then
    [ -d "$_www" ] && [ ! -L "$_www" ] || {
        rm -f "$_version_backup" "$_version_missing" "$_backup"
        echo "relayway: existing Web directory is not a regular directory; refusing upgrade" >&2
        exit 1
    }
    if ! mv "$_www" "$_www_backup" || ! mkdir -m 0755 "$_www"; then
        mv "$_www_backup" "$_www" 2>/dev/null || true
        rm -f "$_version_backup" "$_version_missing" "$_backup"
        echo "relayway: could not preserve the current Web directory; refusing upgrade" >&2
        exit 1
    fi
else
    : > "$_www_missing" || {
        rm -f "$_version_backup" "$_version_missing" "$_backup"
        echo "relayway: could not record missing Web directory; refusing upgrade" >&2
        exit 1
    }
fi
exit 0
